﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CNVP.JShop.Web.Controls
{
    public partial class DeliveryFreight : System.Web.UI.Page
    {
        public string ProductIDS = "";
        public string ListProvince = "";
        public string SaleNumArray = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            SaleNumArray = Request.QueryString["SaleNumArray"];//销售数量
            ProductIDS = Request.QueryString["ProductIDS"];//主产品IDS
            ListProvince = Request.QueryString["ListProvince"];//详细产品IDS
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(ListProvince))
                {
                    //快递
                    decimal Freight_Decimal_kuaidi = 0;
                    decimal Freight_Decimal_EMS = 0;
                    decimal Freight_Decimal_Pingyou = 0;

                    string Kuaidi = DeliveryFreightInfo("快递", 1, ref Freight_Decimal_kuaidi);
                    string EMS = DeliveryFreightInfo("EMS", 2, ref Freight_Decimal_EMS);
                    string Pingyou = DeliveryFreightInfo("平邮", 3, ref Freight_Decimal_Pingyou);
                    string AddTr = "";
                    if (Freight_Decimal_kuaidi == 0 && Freight_Decimal_EMS == 0 && Freight_Decimal_Pingyou == 0)
                    {
                        AddTr += "				 卖家承担运费";//物流名称
                        AddTr += "<select id=\"DeliveryWayPrice\" name=\"DeliveryWayPrice\" class=\"Display\">";
                        AddTr += " <option value=\"卖家承担运费:0\">卖家承担运费:0</option>";
                        AddTr += " </select>";
                        Response.Write(AddTr);
                    }
                    else if (Freight_Decimal_EMS == 0 && Freight_Decimal_Pingyou == 0)
                    {
                        AddTr += "<select id=\"DeliveryWayPrice\" name=\"DeliveryWayPrice\">";
                        AddTr += Kuaidi;
                        AddTr += " </select>";
                        Response.Write(AddTr);
                    }
                    else if (Freight_Decimal_kuaidi == 0 && Freight_Decimal_EMS == 0)
                    {
                        AddTr += "<select id=\"DeliveryWayPrice\" name=\"DeliveryWayPrice\">";
                        AddTr += Pingyou;
                        AddTr += " </select>";
                        Response.Write(AddTr);
                    }
                    else if (Freight_Decimal_kuaidi == 0 && Freight_Decimal_Pingyou == 0)
                    {
                        AddTr += "<select id=\"DeliveryWayPrice\" name=\"DeliveryWayPrice\">";
                        AddTr += EMS;
                        AddTr += " </select>";
                        Response.Write(AddTr);
                    }
                    else if (Freight_Decimal_kuaidi == 0)
                    {
                        AddTr += "<select id=\"DeliveryWayPrice\" name=\"DeliveryWayPrice\" onchange=\"DeliveryWayCheck()\">";
                        AddTr += EMS;
                        AddTr += Pingyou;
                        AddTr += " </select>";
                        Response.Write(AddTr);
                    }
                    else if (Freight_Decimal_EMS == 0)
                    {
                        AddTr += "<select id=\"DeliveryWayPrice\" name=\"DeliveryWayPrice\" onchange=\"DeliveryWayCheck()\">";
                        AddTr += Kuaidi;
                        AddTr += Pingyou;
                        AddTr += " </select>";
                        Response.Write(AddTr);
                    }
                    else if (Freight_Decimal_Pingyou == 0)
                    {
                        AddTr += "<select id=\"DeliveryWayPrice\" name=\"DeliveryWayPrice\" onchange=\"DeliveryWayCheck()\">";
                        AddTr += Kuaidi;
                        AddTr += EMS;
                        AddTr += " </select>";
                        Response.Write(AddTr);
                    }
                    else
                    {
                        AddTr += "<select id=\"DeliveryWayPrice\" name=\"DeliveryWayPrice\" onchange=\"DeliveryWayCheck()\">";
                        AddTr += Kuaidi;
                        AddTr += EMS;
                        AddTr += Pingyou;
                        AddTr += " </select>";
                        Response.Write(AddTr);
                    }

                }
                else
                {
                    Response.Write("<font color=\"red\">请输入收货信息确认！</font>");
                }
            }
           
        }
        private string DeliveryFreightInfo(string Delivery_Str, byte DeliveryType_Byte, ref decimal Freight_Decimal)
        {
            BLL.CNVP_Products bll=new BLL.CNVP_Products ();
            string AddTr = "";
            decimal freight = 0;
            if (!string.IsNullOrEmpty(SaleNumArray) && !string.IsNullOrEmpty(ProductIDS) && !string.IsNullOrEmpty(ListProvince))
            {
                var list1 = bll.GetDeliveryFreightID_CNVP_Products_InProductIDSBroupBy(ProductIDS);
                if (list1 != null && list1.Count > 0)
                {
                    foreach (Model.CNVP_Products m1 in list1)
                    {
                        int SumNum = 0;//每个模板产品数量
                        string[] PIDSArray = ProductIDS.Split(Convert.ToChar(","));
                        for (int i = 0; i < PIDSArray.Length; i++)
                        {
                            
                                int Amount_Int = 0;
                                string[] NumArray = SaleNumArray.Split(Convert.ToChar(","));
                                Amount_Int = Convert.ToInt32(NumArray[i]);//销售数量
                                var model = new BLL.CNVP_Products().GetSingle_CNVP_ProductsByIdAndDeliveryFreightID(Convert.ToInt32(PIDSArray[i]), m1.DeliveryFreightID);
                                if (model != null)
                                {
                                    if (model.DeliveryFreightID != 0)//注意排除卖家承担
                                    {
                                        SumNum += Amount_Int;
                                    }
                                }
                        }
                        //string xx = SumNum.ToString();
                        bool falg = false;
                        var list_DeliveryFreight = new BLL.CNVP_DeliveryFreight().LoadAllByDeliveryFreightColumnID(m1.DeliveryFreightID, DeliveryType_Byte, "order by IsDefault");
                        if (list_DeliveryFreight != null && list_DeliveryFreight.Count > 0)
                        {
                            for (int j = 0; j < list_DeliveryFreight.Count; j++)
                            {
                                if (falg)
                                {
                                    break;
                                }
                                if (!string.IsNullOrEmpty(list_DeliveryFreight[j].ProvinceArray))
                                {
                                    string[] arr = list_DeliveryFreight[j].ProvinceArray.Split(Convert.ToChar("|"));//省份Code
                                    for (int k = 0; k < arr.Length; k++)
                                    {
                                        #region 循环1
                                        //判断哪个省
                                        if (arr[k] == ListProvince)
                                        {
                                            if (SumNum == list_DeliveryFreight[j].FirstPiece)
                                            {
                                                freight += list_DeliveryFreight[j].FirstFreight;
                                            }
                                            else if (SumNum > list_DeliveryFreight[j].FirstPiece)
                                            {
                                                int Num = 0;
                                                Num = SumNum - list_DeliveryFreight[j].FirstPiece;
                                                freight += list_DeliveryFreight[j].FirstFreight + (Num / list_DeliveryFreight[j].AddPiece) * list_DeliveryFreight[j].AddFreight;
                                            }
                                            else
                                            {
                                                freight += list_DeliveryFreight[j].FirstFreight;
                                            }
                                            falg = true;
                                            break;
                                        }
                                        #endregion
                                    }
                                }
                                else
                                {
                                    if (SumNum == list_DeliveryFreight[j].FirstPiece)
                                    {
                                        freight += list_DeliveryFreight[j].FirstFreight;
                                    }
                                    else if (SumNum > list_DeliveryFreight[j].FirstPiece)
                                    {
                                        int Num = 0;
                                        Num = SumNum - list_DeliveryFreight[j].FirstPiece;
                                        freight += list_DeliveryFreight[j].FirstFreight + (Num / list_DeliveryFreight[j].AddPiece) * list_DeliveryFreight[j].AddFreight;
                                    }
                                    else
                                    {
                                        freight += list_DeliveryFreight[j].FirstFreight;
                                    }
                                    falg = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                Freight_Decimal = freight;
                AddTr += "<option value=\"" + Delivery_Str + ":" + freight + "\">" + Delivery_Str + ":" + freight + "</option>";
            }
            return AddTr;
        }
    }
}